<?php

class ImageInfrastructure 
{
	const STATUS_ACTIVE = 1;
	const STATUS_DELETED = -1;
	
	#public static function GetWritableRecordFactory()
	#{
	#	$config = ConfigReaderFactory::getInstance()->getConfigReader(ImageInfrastructureServerConfig::CONFIG_FILE)->getConfig();
	#	$db = $config->db->writer;
	#	$user = $config->db->user;
	#	$pass = $config->db->pwd;
	#	$name = $config->db->name;
	#	return new WritableRecordFactory($db, $user, $pass, $name);
	#}

	#public static function GetReadableRecordFactory()
	#{
	#	$config = ConfigReaderFactory::getInstance()->getConfigReader(ImageInfrastructureServerConfig::CONFIG_FILE)->getConfig();
	#	$readers = array();
	#	foreach ($config->db->readers->reader as $reader) {
	#		$readers[] = (string) $reader;
	#	}
	#	$i = array_rand($readers);

	#	$db = $readers[$i];
	#	$user = $config->db->user;
	#	$pass = $config->db->pwd;
	#	$name = $config->db->name;
	#	return new ReadableRecordFactory($db, $user, $pass, $name);
	#}

	public static function upload($user_id, $tmp_name, $original_name)
	{
		try {
			return self::__upload($user_id, $tmp_name, $original_name);
		} catch (IIException $e) {
			error_log('Error - IIException:');
			error_log('Message - ' . $e->getMessage());
			error_log('File - ' . $e->getFile());
			error_log('Line - ' . $e->getLine());
			error_log($e->getTraceAsString());
			//error_log(var_export($e->getTrace(), 1));
			return $e->getCode();
		}
	}

	private static function __upload($user_id, $tmp_name, $original_name)
	{
		// validate image
		$image_info = @getimagesize($tmp_name);
		if (empty($image_info)) {
			// non-image
			throw new IIException("Upload a non-image file: {$original_name}, {$tmp_name}", -1);
		}

		$source = new Source;
		$source->width = $image_info[0];
		$source->height = $image_info[1];
		$source->farm = self::__getCurrentFarm();
		$source->status = 1;
		$source->version = 1;
		$source->format = $image_info[2];
		$source->original_name = $original_name;
		$source->uploaded_by = $user_id;
		$source->id = $source->insert();
		if (!$source->id) {
			// create source record fails
			throw new IIException("Create source record FAILS: " . var_export($source), -2);
		}
	
		// keep it well
		$filename = self::__getUploadStorage($source->id);
		$dir = dirname($filename);
		$ret = @mkdir($dir, 0777, true);
		$ret = @chmod($dir, 0777);
		$ret = copy($tmp_name, $filename);

		// new export entry
		$export = new Export();
		$export->source_id = $source->id;
		$export->width = 136;
		$export->height = 76;
		$export->resize_mode = ImageHelper::PAD;
		$export->watermark = '';
		$export->format = 2;
		$export->export_arg = 85;
		$export->timestamp = time();
		$export->farm = self::__getCurrentFarm();
		$export->ready = 0;
		$export->version = 1;
		$export->id = $export->insert();	
		if (!$export->id) {
			// create export record fails
			throw new IIException("Create export record FAILS: " . var_export($export), -3);
		}

		$ret = self::export($export->id);
		// TODO : ERROR Handling
		
		$preview_url = self::GetExportImageUrlEasily($export->id);			
		$selfinfo = array(
			'source_id' => $source->id,
			'preview_url' => $preview_url
		);
			
		return $selfinfo;
	}

	#static public function RequestMultipleExport($export_list) {
	#	$result = array();
	#	foreach ($export_list as $k => $v) {
	#		if (sizeof($v) != 9) {
	#			$result[] = -1; // invalid argument
	#		} else {
	#			$result[] = self::RequestDistribute($v[0], $v[1], $v[2], $v[3], $v[4], $v[5], $v[6], $v[7], 0, $v[8]);
	#		}
	#	}
	#	
	#	return $result;
	#}

	#static public function RequestExport($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $callback_url) {
	#	return self::RequestDistribute($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, 0, $callback_url);
	#}

	#static public function RequestMultipleDistribute($distribute_list) {
	#	$result = array();
	#	foreach ($distribute_list as $k => $v) {
	#		if (sizeof($v) != 10) {
	#			$result[] = -1;
	#		} else {
	#			$result[] = self::RequestDistribute($v[0], $v[1], $v[2], $v[3], $v[4], $v[5], $v[6], $v[7], $v[8], $v[9]);
	#		}
	#	}
	#	
	#	return $result;
	#}

	#static public function RequestDistribute($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $destination_id, $callback_url) {
	#	$rf = self::GetWritableRecordFactory();
	#	
	#	if (($width <= 0) || ($height <= 0) || ($source_id <= 0)) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Invalid input parameters FAILS");
	#		error_log("Parameters: width={$width}, height={$height}, source_id={$source_id}");
	#		
	#		return -1;
	#	}

	#	// check existing export
	#	$export = self::GetExport($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg);
	#	if (!$export) {
	#		// new export entry
	#		$export = new Export();
	#		$export->source_id = $source_id;
	#		$export->width = $width;
	#		$export->height = $height;
	#		$export->resize_mode = $resize_mode;
	#		$export->watermark = $watermark;
	#		$export->format = $format;
	#		$export->export_arg = $export_arg;
	#		$export->timestamp = time();
	#		$export->farm = self::__getCurrentFarm();
	#		$export->ready = 0;
	#		$export->version = 1;
	#		$export->id = $rf->insert($export);		
	#		if (!$export->id) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - Create export record FAILS");
	#			error_log("Parameters: " . var_export($export));
	#			
	#			return -1; // create export record fails
	#		}
	#	}

	#	// enter export queue
	#	list($broker, $queue) = self::getExportQueueInfo();
	#	$enqueue_ret = MessageHelper::sendMessage($broker, $queue, array(
	#		'login_id' => $login_id,
	#		'export_id' => $export->id,
	#		'destination_id' => $destination_id,
	#		'callback_url' => $callback_url
	#	));
	#	
	#	if (!$enqueue_ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Insert export queue FAILS");
	#		$parameters = array($broker, $queue, array(
	#				'login_id' => $login_id,
	#				'export_id' => $export->id,
	#				'destination_id' => $destination_id,
	#				'callback_url' => $callback_url
	#			)
	#		);			
	#		error_log("Parameters: " . var_export($parameters));
	#		
	#		return -2; // Insert export queue FAILS
	#	}

	#	return $export->id;
	#}

	#static public function AddMeta($source_id, $login_id, $title, $person, $tag, $miscellaneous) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);
	#	$found = $rf->find('Meta', 'source_id=?', array($source_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new Meta();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// add meta
	#	$meta_entry->addValueToJsonArray('title', $title);
	#	$meta_entry->addValueToJsonArray('person', $person);
	#	$meta_entry->addValueToJsonArray('tag', $tag);
	#	$meta_entry->addHashToJsonHash('miscellaneous', $miscellaneous);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id'));
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Add meta FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}

	#static public function UpdateMeta($source_id, $login_id, $title, $person, $tag, $miscellaneous) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);
	#	$found = $rf->find('Meta', 'source_id=?', array($source_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new Meta();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->title = json_encode($title);
	#	$meta_entry->person = json_encode($person);
	#	$meta_entry->tag = json_encode($tag);
	#	$meta_entry->miscellaneous = json_encode($miscellaneous);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}

	#static public function QueueDistribute($login_id, $export_id, $destination_id, $callback_url) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$export = $rf->findInstance('Export', $export_id);
	#	if (!$export) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Export entry not found: export_id = {$export_id}");
	#		
	#		return -1; //Export entry not found
	#	}
	#	
	#	$destination_entry = $rf->findInstance('Destination', $destination_id);
	#	if (!$destination_entry) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Destination entry not found: destination_id = {$destination_id}");
	#		
	#		return -2; //Destination entry
	#	}
	#	
	#	$found = $rf->find('Distribution',
	#		'export_id=? AND destination_id=?',
	#		array($export_id, $destination_id)
	#	);

	#	if ($found) {
	#		$distribution_entry = array_shift($found);			
	#	} else {
	#		// create new distribution record
	#		$distribution_entry = new Distribution();
	#		$distribution_entry->export_id = $export_id;
	#		$distribution_entry->destination_id = $destination_id;
	#		$distribution_entry->distributed_by = $login_id;
	#		$distribution_entry->status = 0;
	#		$distribution_entry->id = $rf->insert($distribution_entry);
	#		if ($distribution_entry->id != 0) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - Create distribution record FAILS");
	#			error_log("Parameters: " . var_export($distribution_entry));
	#			
	#			return -3; // Create destination record FAILS
	#		}
	#	} 

	#	// enter distribution queue
	#	if ($destination_entry->broker) {
	#		$broker = $destination_entry->broker;
	#		$queue = $destination_entry->queue;
	#		$ret = MessageHelper::sendMessage($broker, $queue,
	#			array('export_id' => $export_id, 'callback_url' => $callback_url)
	#		);
	#			
	#		if (!$ret) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - sendMessage FAILS");
	#			error_log("MessageHelper::sendMessage({$broker}, {$queue}, array('export_id' => {$export_id}, 'callback_url' => {$callback_url})");
	#			
	#			return -4; // Send Message FAILS
	#		}
	#	}
	#	
	#	return TRUE;
	#}

	public static function export($export_id)
	{
		$export_detail = new Export;
		$export_detail = $export_detail->get($export_id);

		if (empty($export_detail)) {
			return FALSE;
		} else if ($export_detail->ready) { // skip exporting if its been done
			return TRUE;
		}

		$source_detail = new Source;
		$source_detail = $source_detail->get($export_detail->source_id);
		if ($source_detail->status != self::STATUS_ACTIVE) {
			return -3;
		}

		// get export parameters
		$export_file = self::__getExportedStorage($export_id, $export_detail->farm);
		$source_file = self::__getSourceStorage($source_detail->id, $source_detail->farm);

		// error checking on files existence starts
		if (!file_exists($source_file)) {
			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
			error_log("ERROR - Missing source file: {$source_file}");
			return -1;   
		}

		$width = $export_detail->width;
		$height = $export_detail->height;
		$resize_mode = $export_detail->resize_mode;
		$watermark = $export_detail->watermark;

		$source_width = $source_detail->width;
		$source_height = $source_detail->height;
		
		// check if the export size > source size
		if( max($width, $height) >  max($source_width, $source_height) ) {
			$check_width = $source_width;
			$check_height = $source_height;
		} else {
			$check_width = $width;
			$check_height = $height;	
		}
		
		// export image
		$dir = dirname($export_file);
		$ret = @mkdir($dir, 0777, true);
		$ret = @chmod($dir, 0777);
		
		// resolve watermark id->corresponding size's watermark 
		if($watermark) {
			$watermark_pattern_entry = $rf->find('WatermarkPattern', 'watermark_id=?', array($watermark));
			if( count($watermark_pattern_entry)>0 ) {
				foreach($watermark_pattern_entry as $watermark_pattern) {
					if ( $check_width > $check_height ) {
						if( $check_width >= $watermark_pattern->width_from && $check_width <= $watermark_pattern->width_to ) {
							$watermark_file = $watermark_pattern->filename;
							break;
						}
					} else {
						if( $check_height >= $watermark_pattern->height_from && $check_height <= $watermark_pattern->height_to ) {
							$watermark_file = $watermark_pattern->filename;
							break;
						}
					}
				}
				$watermark = self::GetWatermarkFile($watermark, $watermark_file);
				if( !is_file($watermark) ) {
					error_log("ERROR - Watermark : requested watermark ,but watermark file not found !");
					return -3;
				}
			} else {
				error_log("ERROR - Watermark ID not not found !");
				return -4;
			}
		}
		
		ImageHelper::Export($source_file, $export_file, $width, $height, $resize_mode, $watermark);

		$export_detail->version = $source_detail->version;
		$export_detail->ready = ($ret) ? self::STATUS_ACTIVE : self::STATUS_DELETED;
		$ret = $export_detail->update();
		if (!$ret) {
			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
			error_log("ERROR - Update export entry record FAILS");
			error_log("Parameters: " . var_export($export));
			
			return -2;
		}
		
		return 0;
	}

	#static public function Callback($export_id, $destination_id, $status, $callback_url) {
	#	if (!$callback_url) {
	#		return TRUE;
	#	}

	#	$rf = self::GetReadableRecordFactory();
	#	$export = $rf->findInstance('Export', $export_id);
	#	if (!$export) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Export entry not found: export_id = {$export_id}");
	#		
	#		return -2;
	#	}
	#	
	#	$http_path = self::__getHttpPath($export_id, $destination_id);
	#	
	#	$request = new HttpRequest($callback_url, HttpRequest::METH_POST);
	#	$key = ConfigReaderFactory::getInstance()->getConfigReader(ImageInfrastructureServerConfig::WEB_SERVICE_CONFIG_FILE)->getConfig()->webservice->key;
	#	$signature = sha1($key.$export_id.$destination_id.$status);
	#	$parameters = array(
	#		'source_id' => $export->source_id,
	#		'export_id' => $export_id,
	#		'destination_id' => $destination_id,
	#		'status' => $status,
	#		'signature' => $signature,
	#		'http_path' => $http_path
	#	);
	#	$request->addPostFields($parameters);
	#	
	#	try {
	#		$ret = $request->send();
	#	} catch (Exception $e) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Create export entry record FAILS");
	#		error_log("Parameters: " . var_export($parameters));
	#		error_log($e->getMessage());
	#		error_log($e->getTraceAsString());
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}

	#static public function GetSource($source_id) {
	#	$rf = self::GetWritableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);		
	#	return $source_entry;
	#}
	
	#static public function GetExport($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg) {
	#	$rf = self::GetReadableRecordFactory();

	#	// check export record
	#	$found = $rf->find(
	#		'Export',
	#		'source_id=? AND width=? AND height=? AND resize_mode=? AND watermark=? AND format=? AND export_arg=?',
	#		array($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg)
	#	);

	#	$export = array_shift($found);

	#	return $export;
	#}
	
	#static public function GetExportById($exportId) {
	#	$rf = self::GetWritableRecordFactory();
	#	
	#	$export = $rf->findInstance('Export', $exportId);		
	#	
	#	return $export;
	#}

	#static public function QueryStatusTroublesomely($source_id, $width, $height, $resize_mode, $watermark, $destination_id, $format = 2, $export_arg = 85) {
	#	$export = self::GetExport($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg);
	#	if (!is_object($export)) {
	#		return FALSE; // return "No export record found";
	#	}
	#	return self::QueryStatusEasily($export->id, $destination_id);
	#}

	#static public function QueryStatusEasily($export_id, $destination_id) {
	#	$rf = self::GetReadableRecordFactory();

	#	// check export record
	#	$export = $rf->findInstance('Export', $export_id);
	#	if (!$export) {
	#		return -1; // return "No export record found"
	#	}
	#	
	#	// check export status
	#	switch ($export->ready) {
	#	case 0: 
	#		return 2;// return "Exporting"

	#	case self::STATUS_DELETED: 				
	#		return -1;//Error: $distribution_entry->other_info
	#	}
	#	
	#	// check distribution record
	#	$found = $rf->find(
	#		'Distribution',
	#		'export_id=? AND destination_id=?',
	#		array($export->id, $destination_id)
	#	);

	#	if (!$found) {
	#		return -1;	//return "No distribution record found"
	#	}
	#	$distribution_entry = array_shift($found);

	#	// check distribution status
	#	switch ($distribution_entry->status) {
	#		case '1': 
	#			return self::__getHttpPath($export_id, $destination_id);
	#			
	#		case '0': 
	#			return 0;//Waiting for distribution
	#			
	#		case '-1': 
	#			return -1;//Error: $distribution_entry->other_info
	#	}
	#}

	#static public function GetExportQueueInfo() {
	#	$config = ConfigReaderFactory::getInstance()->getConfigReader(ImageInfrastructureServerConfig::CONFIG_FILE)->getConfig();
	#	$broker = $config->export->broker;
	#	$queue = $config->export->queue;

	#	return array($broker, $queue);
	#}

	private static function __getCurrentFarm()
	{
		$config = ConfigReaderFactory::getInstance()->getConfigReader(ImageInfrastructureServerConfig::CONFIG_FILE)->getConfig();
		
		return $config->imageFarm;
	}

	private static function __getUploadStorage($id)
	{
		$farm = self::__getCurrentFarm();
		$hash = ceil($id / 100);
		$dir = "/data/image_infrastructure/image/$farm/source/$hash";
		$filename = sprintf("%012d.jpg", $id);
		
		return "$dir/$filename";
	}

	#static public function GetWatermarkFile($watermark, $watermark_file) {
	#	$dir = "/data/image_infrastructure/watermark";
	#	
	#	return  $dir."/".$watermark."/".$watermark_file;
	#}

	public static function __getSourceStorage($id, $farm)
	{
		$hash = ceil($id / 100);
		$dir = "/data/image_infrastructure/image/$farm/source/$hash";
		$filename = sprintf("%012d.jpg", $id);
		
		return "$dir/$filename";
	}

	#static public function GetExportStorage($id) {
	#	$farm = self::__getCurrentFarm();
	#	$hash = ceil($id / 100);
	#	$dir = "/data/image_infrastructure/image/$farm/export/$hash";
	#	$filename = sprintf("%012d.jpg", $id);
	#	
	#	return "$dir/$filename";
	#}

	private static function __getExportedStorage($id, $farm)
	{
		$hash = ceil($id / 100);
		$dir = "/data/image_infrastructure/image/$farm/export/$hash";
		$filename = sprintf("%012d.jpg", $id);
		
		return "$dir/$filename";
	}

	#static public function GetDistributedStorage($export_id, $destination_id) {
	#	$rf = self::GetReadableRecordFactory();
	#	$export = $rf->findInstance('Export', $export_id);
	#	if (!$export) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Export entry not found: export_id = {$export_id}");
	#		
	#		return FALSE;
	#	}
	#	
	#	$destination_entry = $rf->findInstance('Destination', $destination_id);
	#	if (!$destination_entry) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Destination entry not found: destination_id = {$destination_id}");
	#		
	#		return FALSE;
	#	}
	#	 
	#	$farm = $export->farm;
	#	$hash = ceil($export_id / 100);
	#	$filename = sprintf("%012d_%10d", $export_id, $export->timestamp);
	#	eval("\$path = \"$destination_entry->local_path\";");
	#	
	#	return $path;
	#}

	private static function __getHttpPath($export_id, $destination_id)
	{
		$export = new Export;
		$export = $export->get($export_id);
		if (!$export) {
			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
			error_log("ERROR - Export entry not found: export_id = {$export_id}");
			return FALSE;
		}
		
		$destination = new Destination;
		$destination_detail = $destination->get($destination_id); 
		if (!$destination_detail) {
			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
			error_log("ERROR - Destination entry not found: destination_id = {$destination_id}");
			
			return FALSE;
		}

		$farm = $export->farm;
		$hash = ceil($export_id / 100);
		$export_id = sprintf("%012d", $export_id);
		$timestamp = sprintf("%010d", $export->timestamp);

		eval("\$path = \"$destination_detail->http_path\";");
		return $path;
	}

	#static public function GetSourceImageUrl($source_id) {
	#	$rf = self::GetReadableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);
	#	if (!$source_entry) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Source entry not found: source_id = {$source_id}");
	#		
	#		return -1; // No source record found
	#	}
	#	
	#	$farm = $source_entry->farm;
	#	$hash = ceil($source_id / 100);		
	#	$url_prefix = "http://ii.tvb.com/image/$farm/source/$hash";
	#	$filename = sprintf("%012d.jpg", $source_id);
	#	
	#	return "$url_prefix/$filename";
	#}
	
	//output url for ii
	public static function GetExportImageUrlEasily($export_id)
	{
		$export = new Export;
		$export = $export->get($export_id);

		if (!$export) {
			return -1; //"No export record found"
		}
		
		// check export status
		switch ($export->ready) {
			case self::STATUS_ACTIVE:
				return self::__getHttpPath($export_id, 0);
			case self::STATUS_DELETED:
				return self::STATUS_DELETED;
			default:
				return 2; //"Exporting"
		}		
	}

	#static public function GetExportImageUrlTroublesomely($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg) {
	#	$export = self::GetExport($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg);
	#	if (!$export) {
	#		return -1; //"No export record found"
	#	}
	#	
	#	// check export status
	#	$status_code = $export->ready;
	#	if ($status_code != '1') {
	#		return 2; //"Exporting"
	#	}

	#	return self::__getHttpPath($export->id, 0);
	#}
	
	#static public function UpdateAcl($source_id, $destination_id, $login_id, $start_time, $end_time, array $country_allow_list, array $country_deny_list) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('Acl', 'source_id=? AND destination_id=?', array($source_id, $destination_id));

	#	// create new acl record when necessary
	#	if ($found) {
	#		$acl_entry = array_shift($found);
	#	} else {
	#		$acl_entry = new Acl();
	#		$acl_entry->source_id = $source_id;
	#		$acl_entry->destination_id = $destination_id;
	#		$acl_entry->create_time = date('c');
	#		$acl_entry->creator = $login_id; 
	#	}

	#	// update acl
	#	$acl_entry->start_time = date('c', $start_time);
	#	$acl_entry->end_time = date('c', $end_time);
	#	$acl_entry->country_allow_list = json_encode($country_allow_list);
	#	$acl_entry->country_deny_list = json_encode($country_deny_list);
	#	$acl_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($acl_entry, array('source_id', 'destination_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update acl FAILS");
	#		error_log("Parameters: " . var_export($acl_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function UpdateMetaManual($source_id, $login_id, array $manual_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaManual', 'source_id=?', array($source_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaManual();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->manual_tags = json_encode($manual_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-manual FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaManual($source_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaManual', 'source_id=?', array($source_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id'));
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-manual entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaArtiste($source_id, $artiste_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaArtiste', 'source_id=? AND artiste_id=?', array($source_id, $artiste_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaArtiste();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->artiste_id = $artiste_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'artiste_id'));
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-artiste FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaArtiste($source_id, $artiste_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaArtiste', 'source_id=? AND artiste_id=?', array($source_id, $artiste_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'artiste_id'));
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-artiste entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaBFarm($source_id, $blog_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaBFarm', 'source_id=? AND blog_id=?', array($source_id, $blog_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaBFarm();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->blog_id = $blog_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'blog_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-bfarm FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaBFarm($source_id, $blog_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaBFarm', 'source_id=? AND blog_id=?', array($source_id, $blog_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'blog_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-bfarm entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaBlog($source_id, $blog_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaBlog', 'source_id=? AND blog_id=?', array($source_id, $blog_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaBlog();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->blog_id = $blog_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'blog_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-blog FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaBlog($source_id, $blog_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaBlog', 'source_id=? AND blog_id=?', array($source_id, $blog_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'blog_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-blog entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaCast($source_id, $cast_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaCast', 'source_id=? AND cast_id=?', array($source_id, $cast_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaCast();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->cast_id = $cast_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'cast_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-cast FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaCast($source_id, $cast_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaCast', 'source_id=? AND cast_id=?', array($source_id, $blog_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'cast_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-cast entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaProgramme($source_id, $programme_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaProgramme', 'source_id=? AND programme_id=?', array($source_id, $programme_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaProgramme();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->programme_id = $programme_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'programme_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-programme FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaProgramme($source_id, $programme_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaProgramme', 'source_id=? AND programme_id=?', array($source_id, $programme_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'programme_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-programme entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function UpdateMetaEpisode($source_id, $episode_id, $login_id, array $system_tags) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaEpisode', 'source_id=? AND episode_id=?', array($source_id, $episode_id));

	#	// create new meta record when necessary
	#	if ($found) {
	#		$meta_entry = array_shift($found);
	#	} else {
	#		$meta_entry = new MetaEpisode();
	#		$meta_entry->source_id = $source_id;
	#		$meta_entry->episode_id = $episode_id;
	#		$meta_entry->create_time = date('c');
	#		$meta_entry->creator = $login_id; 
	#	}

	#	// update meta
	#	$meta_entry->system_tags = json_encode($system_tags);
	#	$meta_entry->modifier = $login_id;

	#	// save
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'episode_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Update meta-episode FAILS");
	#		error_log("Parameters: " . var_export($meta_entry, TRUE));
	#	}
	#	
	#	return $ret;
	#}
	
	#static public function DeleteMetaEpisode($source_id, $episode_id, $login_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$found = $rf->find('MetaProgramme', 'source_id=? AND episode_id=?', array($source_id, $episode_id));

	#	if (!$found) {
	#		return TRUE;
	#	}
	#	$meta_entry = array_shift($found);

	#	// update meta
	#	$meta_entry->modifier = $login_id;
	#	$meta_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($meta_entry, array('source_id', 'episode_id'));		
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted meta-episode entry record FAILS");
	#		error_log("Parameters: " . var_export($meta_entry));
	#		
	#		return -1;
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function DeleteSource($source_id) {
	#	// get db records
	#	$rf = self::GetWritableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);
	#
	#	if (!$source_entry) {
	#		return TRUE; // no source is found
	#	}
	#	
	#	$source_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save($source_entry);
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted source entry record FAILS");
	#		error_log("Parameters: " . var_export($source_entry));
	#		
	#		return -1;
	#	}
	#	
	#	
	#	$export_list = $rf->find('Export', 'source_id=?', array($source_id));
	#	foreach ($export_list as $export) {
	#		$export->ready = self::STATUS_DELETED;
	#		$ret = $rf->save($export);
	#		if (!$ret) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - Mark-Deleted export entry record FAILS");
	#			error_log("Parameters: " . var_export($export));
	#			
	#			return -2;
	#		}
	#		
	#		$distribution_entry_list = $rf->find('Distribution', 'export_id=?', array($export->id));
	#		foreach ($distribution_entry_list as $distribution_entry) {
	#			$distribution_entry->status = self::STATUS_DELETED;
	#			$ret = $rf->save_by_fields($distribution_entry, array('export_id', 'destination_id'));
	#			if (!$ret) {
	#				error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#				error_log("ERROR - Mark-Deleted distribution entry record FAILS");
	#				error_log("Parameters: " . var_export($distribution_entry));
	#				
	#				return -3;
	#			}
	#			
	#			$ret = self::QueueRecall($distribution_entry->export_id, $distribution_entry->destination_id);
	#		}
	#	}
	#	
	#	return $ret;
	#}

	#static public function QueueRecall($export_id, $destination_id) {
	#	// get db record
	#	$rf = self::GetWritableRecordFactory();
	#	$export = $rf->findInstance('Export', $export_id);
	#	if (!$export) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Export entry not found: export_id = {$export_id}");
	#		
	#		return -1; //Export entry not found
	#	}
	#	
	#	$destination_entry = $rf->findInstance('Destination', $destination_id);
	#	if (!$destination_entry) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Destination entry not found: destination_id = {$destination_id}");
	#		
	#		return -2; //Destination entry
	#	}
	#	
	#	$found = $rf->find('Distribution', 'export_id=? AND destination_id=?', array($export->id, $destination_entry->id));
	#	if (!$found) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Distribution entry not found: export_id = {$export_id}, destination_id = {$destination_id}");
	#		
	#		return -5; //Destination entry
	#	}
	#	$distribution_entry = array_shift($found);
	#	
	#	$distribution_entry->status = self::STATUS_DELETED;
	#	$ret = $rf->save_by_fields($distribution_entry, array('export_id', 'destination_id'));
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Mark-Deleted distribution entry record FAILS");
	#		error_log("Parameters: " . var_export($distribution_entry));
	#		
	#		return -3;
	#	}
	#			
	#	// enter distribution queue
	#	if ($destination_entry->broker) {
	#		$broker = $destination_entry->broker;
	#		$queue = $destination_entry->recall_queue;
	#		$ret = MessageHelper::sendMessage($broker, $queue,
	#			array('export_id' => $export_id)
	#		);
	#			
	#		if (!$ret) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - sendMessage FAILS");
	#			error_log("MessageHelper::sendMessage({$broker}, {$queue}, array('export_id' => {$export_id}, 'callback_url' => {$callback_url})");
	#			
	#			return -4; // Send Message FAILS
	#		}
	#	}
	#	
	#	return TRUE;
	#}
	
	#static public function RequestRecall($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $destination_id) {
	#	$rf = self::GetWritableRecordFactory();
	#	
	#	if (($width <= 0) || ($height <= 0) || ($source_id <= 0)) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Invalid input parameters FAILS");
	#		error_log("Parameters: width={$width}, height={$height}, source_id={$source_id}");
	#		
	#		return -1;
	#	}

	#	// check existing export
	#	$export = self::GetExport($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg);				
	#	if (!$export) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Export entry not found: source_id = $source_id, width = $width, height = $height, resize_mode = $resize_mode, watermark = $watermark, format = $format, export_arg = $export_arg");
	#		
	#		return -2; //Export entry not found
	#	}

	#	$ret = self::QueueRecall($export->id, $destination_id);

	#	return $export->id;
	#}

	#static public function RecoverSource($source_id) {
	#	// get db records
	#	$rf = self::GetWritableRecordFactory();
	#	$source_entry = $rf->findInstance('Source', $source_id);
	#
	#	if (!$source_entry) {
	#		return TRUE; // no source is found
	#	}
	#	
	#	$source_entry->status = self::STATUS_ACTIVE;
	#	$ret = $rf->save($source_entry);
	#	if (!$ret) {
	#		error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#		error_log("ERROR - Recover source entry record FAILS");
	#		error_log("Parameters: " . var_export($source_entry));
	#		
	#		return -1;
	#	}
	#	
	#	
	#	$export_list = $rf->find('Export', 'source_id=?', array($source_id));
	#	foreach ($export_list as $export) {
	#		$export->ready = self::STATUS_ACTIVE;
	#		$ret = $rf->save($export);
	#		if (!$ret) {
	#			error_log("ERROR CLASS:" . __CLASS__ . ", FUNCTION: " . __FUNCTION__ . ", LINE: " . __LINE__);
	#			error_log("ERROR - Recover export entry record FAILS");
	#			error_log("Parameters: " . var_export($export));
	#			
	#			return -2;
	#		}	
	#	}
	#	
	#	return $ret;
	#}
}
?>
